package com.cjy.housePosting.service.impl;

import com.base.common.ConfigConstants;
import com.base.common.enums.AppType;
import com.base.common.enums.HousePoingDetailCategories;
import com.base.common.enums.HousePoingDetailStatus;
import com.base.common.enums.TerminalType;
import com.base.common.persistent.dao.HouseInfoMapper;
import com.base.common.persistent.dao.HousePostingDetailsMapper;
import com.base.common.persistent.dao.HouseRequestMapper;
import com.base.common.persistent.dao.TerminalMapper;
import com.base.common.persistent.model.HouseInfo;
import com.base.common.persistent.model.HousePostingDetails;
import com.base.common.persistent.model.HouseRequest;
import com.base.common.persistent.model.Terminal;
import com.base.common.utils.EasyUtil;
import com.cjy.common.bean.Files;
import com.cjy.housePosting.service.HousePostingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.DecimalFormat;

@Service("housePostingService")
public class HousePostingServiceImpl implements HousePostingService {

	@Resource
	private HousePostingDetailsMapper housePostingDetailsMapper;
	@Resource
	private HouseInfoMapper houseInfoMapper;
	@Resource
	private HouseRequestMapper houseRequestMapper;
	@Resource
	private TerminalMapper terminalMapper;
	@Resource
	private EasyUtil easyUtil;
	
	@Transactional
	@Override
	public int addHousePosting(HousePostingDetails housePostingDetails, Files files, String basePath, String terminalCode, int bid) throws IOException {
		
		int terminalId = 0;
		if (bid == TerminalType.CJY_ANDROID.getValue()) {
			terminalId = terminalMapper.queryTerminalByCodesRegisted(null, null, terminalCode, AppType.CJY.getValue()).get(0).getId();
		} else if(bid == TerminalType.CJY_IOS.getValue()) {
			terminalId = terminalMapper.queryTerminalByCodesRegisted(null, terminalCode, null, AppType.CJY.getValue()).get(0).getId();
		}
		if(terminalId == 0){
			return 0;
		}
		
		Terminal terminal = new Terminal();
		terminal.setId(terminalId);
		housePostingDetails.setPublisher(terminal);
		
		// code前缀
		String pre = "";
		String code = "";
		
		if (HousePoingDetailCategories.SELLING.getValue() == housePostingDetails.getCategories()) {
			pre = "TSA";
			code = generateHousePostingDetailsCode(pre);
			code = pre + code;
		} else if(HousePoingDetailCategories.RENTAL.getValue() == housePostingDetails.getCategories()){
			pre = "TSB";
			code = generateHousePostingDetailsCode(pre);
			code = pre + code;
		} else if(HousePoingDetailCategories.PURCHASE.getValue() == housePostingDetails.getCategories()){
			pre = "TSC";
			code = generateHousePostingDetailsCode(pre);
			code = pre + code;
		}  else if(HousePoingDetailCategories.WANTED.getValue() == housePostingDetails.getCategories()){
			pre = "TSD";
			code = generateHousePostingDetailsCode(pre);
			code = pre + code;
		} else {
			return 0;
		}
		
		housePostingDetails.setCode(code);
		housePostingDetails.setStatus(HousePoingDetailStatus.AUDITING.getValue());
		
		// 图片相对保存地址
		String sysImagesFile = "/resources/images/houseinfo" + "/" + code;
		// 保存图片
		String picUrls = "";
		String[] fileNames = files.getFileFileName();
		if(files.getFile() != null && files.getFile().length > 0){
			picUrls = easyUtil.savePictures(files.getFile(), basePath,
					sysImagesFile, fileNames, picUrls);
		}
		HouseInfo houseInfo = housePostingDetails.getHouseInfo();
		houseInfo.setPicUrls(picUrls);
		houseInfoMapper.addHouseInfo(houseInfo);
		
		houseInfo.setId(houseInfoMapper.queryHouseInfoByCode(houseInfo.getCode()).getId());
		housePostingDetails.setHouseInfo(houseInfo);
		return housePostingDetailsMapper.addHousePostingDetails(housePostingDetails);
	}

	private String generateHousePostingDetailsCode(String pre) {
		String code = housePostingDetailsMapper.queryLastCode(pre);

		DecimalFormat df = new DecimalFormat(ConfigConstants.TICKET_CODE_NUMS);
		if(code == null || "".equals(code.trim())){
			code = df.format(1);
		} else {
			code = df.format(Integer.parseInt(code.substring(3).trim()) + 1);
		}
		return code;
	}

	@Override
	public int queryHousePostingCounts(int categories, int status, String terminalCode, int bid) throws Exception {
		int publisher = 0;
		if(terminalCode != null && !"".equals(terminalCode)){
			if (bid == TerminalType.CJY_ANDROID.getValue()) {
				publisher = terminalMapper.queryTerminalByCodesRegisted(null, null, terminalCode, AppType.CJY.getValue()).get(0).getId();
			} else if(bid == TerminalType.CJY_IOS.getValue()) {
				publisher = terminalMapper.queryTerminalByCodesRegisted(null, terminalCode, null, AppType.CJY.getValue()).get(0).getId();
			}
			if(publisher == 0){
				throw new Exception();
			}
		}
		return housePostingDetailsMapper.queryCountsByCategories(categories, status, publisher);
	}

	@Override
	public Object queryHousePosingList(int page, int categories, int status, String terminalCode, int bid) throws Exception {
		int publisher = 0;
		if(terminalCode != null && !"".equals(terminalCode)){
			if (bid == TerminalType.CJY_ANDROID.getValue()) {
				publisher = terminalMapper.queryTerminalByCodesRegisted(null, null, terminalCode, AppType.CJY.getValue()).get(0).getId();
			} else if(bid == TerminalType.CJY_IOS.getValue()) {
				publisher = terminalMapper.queryTerminalByCodesRegisted(null, terminalCode, null, AppType.CJY.getValue()).get(0).getId();
			}
			if(publisher == 0){
				throw new Exception();
			}
		}
		return housePostingDetailsMapper.queryHousePosingList(((page - 1) * Integer.valueOf(ConfigConstants.INFOS_LIST_NUM.trim())), Integer.parseInt(ConfigConstants.INFOS_LIST_NUM), categories, status, publisher);
	}

	@Override
	public int addHouseRequest(HouseRequest houseRequest, String terminalCode, int bid) {
		
		String code = houseRequestMapper.queryLastCode();

		DecimalFormat df = new DecimalFormat(ConfigConstants.TICKET_CODE_NUMS);
		if(code == null || "".equals(code.trim())){
			code = df.format(1);
		} else {
			code = df.format(Integer.parseInt(code.substring(2).trim()) + 1);
		}
		
		String pre = "TT";
		code = pre + code;
		houseRequest.setCode(code);
		
		int terminalId = 0;
		if (bid == TerminalType.CJY_ANDROID.getValue()) {
			terminalId = terminalMapper.queryTerminalByCodesRegisted(null, null, terminalCode, AppType.CJY.getValue()).get(0).getId();
		} else if(bid == TerminalType.CJY_IOS.getValue()) {
			terminalId = terminalMapper.queryTerminalByCodesRegisted(null, terminalCode, null, AppType.CJY.getValue()).get(0).getId();
		}
		if(terminalId == 0){
			return 0;
		}
		
		Terminal terminal = new Terminal();
		terminal.setId(terminalId);
		houseRequest.setPublisher(terminal);
		return houseRequestMapper.addHouseRequest(houseRequest);
	}

}
